using JT808Protocol.Common;
using JT808Protocol.Models.Attributes;
using JT808Protocol.Models.Enums;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy.DeviceParams
{
    /// <summary>
    /// 单视频参数设置
    /// </summary>
    public class DpAv_0x75 : IDeviceParam
    {
        /// <summary>
        /// 编码模式
        /// </summary>
        [JsonPropertyName("编码模式")] public byte RtpCodecMode { get; set; }

        /// <summary>
        /// 实时流分辨率
        /// </summary>
        [JsonPropertyName("实时流分辨率")] public byte RtpResolutionRatio { get; set; }

        /// <summary>
        /// 实时流关键帧间隔
        /// </summary>
        [JsonPropertyName("实时流关键帧间隔")] public ushort RtpFrameInterval { get; set; }

        /// <summary>
        /// 实时流目标帧率
        /// </summary>
        [JsonPropertyName("实时流目标帧率")] public byte RtpFrameRate { get; set; }

        /// <summary>
        /// 实时流目标码率
        /// </summary>
        [JsonPropertyName("实时流目标码率")] public uint RtpCodecRate { get; set; }

        /// <summary>
        /// 存储编码模式
        /// </summary>
        [JsonPropertyName("存储编码模式")] public byte StorageCodecMode { get; set; }

        /// <summary>
        /// 存储分辨率
        /// </summary>
        [JsonPropertyName("存储分辨率")] public byte StorageResolutionRatio { get; set; }

        /// <summary>
        /// 存储关键帧间隔
        /// </summary>
        [JsonPropertyName("存储关键帧间隔")] public ushort StorageFrameInterval { get; set; }

        /// <summary>
        /// 存储目标帧率
        /// </summary>
        [JsonPropertyName("存储目标帧率")] public byte StorageFrameRate { get; set; }

        /// <summary>
        /// 存储目标码率
        /// </summary>
        [JsonPropertyName("存储目标码率")] public uint StorageCodecRate { get; set; }

        /// <summary>
        /// OSD 字幕叠加设置
        /// </summary>
        [JsonPropertyName("OSD字幕叠加设置")] public ushort OsdSet { get; set; }

        /// <summary>
        /// 是否启用音频输出
        /// </summary>
        [JsonPropertyName("是否启用音频输出")]
        [JsonBooleanString]
        public bool IsAudioOut { get; set; }

        public byte Length => 21 + 5;

        public uint ParamID => 0x75;

        public EnumDeviceParamType ParamType
        {
            get
            {
                try
                {
                    return (EnumDeviceParamType)ParamID;
                }
                catch (Exception)
                {
                    return EnumDeviceParamType.保留;
                }
            }
        }

        public void ReadEntity(ref ReadOnlySpan<byte> source, ref int startIndex)
        {
            throw new NotImplementedException();
        }

        public void WriteToNetBytes(Span<byte> data, ref int startIndex)
        {
            var index = startIndex;
            Array.Copy(ParseHelper.Bytes(ParamID), 0, data.ToArray(), index, 4);

            index += 4;
            data[index] = Length;

            index++;
            data[index] = RtpCodecMode;

            index++;
            data[index] = RtpResolutionRatio;

            index++;
            Array.Copy(ParseHelper.Bytes(RtpFrameInterval), 0, data.ToArray(), index, 2);

            index += 2;
            data[index] = RtpFrameRate;

            index++;
            Array.Copy(ParseHelper.Bytes(RtpCodecRate), 0, data.ToArray(), index, 4);

            index += 4;
            data[index] = StorageCodecMode;

            index++;
            data[index] = StorageResolutionRatio;

            index++;
            Array.Copy(ParseHelper.Bytes(StorageFrameInterval), 0, data.ToArray(), index, 2);

            index += 2;
            data[index] = StorageFrameRate;

            index++;
            Array.Copy(ParseHelper.Bytes(StorageCodecRate), 0, data.ToArray(), index, 4);

            index += 4;
            Array.Copy(ParseHelper.Bytes(OsdSet), 0, data.ToArray(), index, 2);

            index += 2;
            data[index] = IsAudioOut ? (byte)1 : (byte)0;
        }

        public List<byte> WriteToNetBytes()
        {
            List<byte> result = new List<byte>();

            result.AddRange(ParseHelper.Bytes(ParamID));
            result.Add(Length);
            result.Add(RtpCodecMode);
            result.Add(RtpResolutionRatio);
            result.AddRange(ParseHelper.Bytes(RtpFrameInterval));
            result.Add(RtpFrameRate);
            result.AddRange(ParseHelper.Bytes(RtpCodecRate));
            result.Add(StorageCodecMode);
            result.Add(StorageResolutionRatio);
            result.AddRange(ParseHelper.Bytes(StorageFrameInterval));
            result.Add(StorageFrameRate);
            result.AddRange(ParseHelper.Bytes(StorageCodecRate));
            result.AddRange(ParseHelper.Bytes(OsdSet));
            result.Add(IsAudioOut ? (byte)1 : (byte)0);

            return result;
        }
    }
}